package org.cupk2.zhzm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.cupk2.base.db.CupkDbFields;
import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.base.exception.CupkExceptionUtils;
import org.cupk2.zhzm.dao.ZhzmKnowVersionDao;
import org.cupk2.zhzm.domain.ZhzmKnowVersion;
import org.cupk2.zhzm.service.ZhzmKnowVersionServiceInter;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBRule;
import org.cupk2.tools.db.commons.DBRuleList;
import org.cupk2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**知识版本 
 * @author cbtg自动生成  2025-2-4 12:17:07 
 */
@Service
@Slf4j
public class ZhzmKnowVersionServiceImpl implements ZhzmKnowVersionServiceInter {


    @Autowired
    private ZhzmKnowVersionDao zhzmKnowVersionDao;

    @Transactional
    @Override
    public ZhzmKnowVersion insertZhzmKnowVersionEntity(ZhzmKnowVersion zhzmKnowVersion) {
        CupkDbFields.initInsertBean(zhzmKnowVersion, CupkUserContextLoader.getCurrentUser());

        zhzmKnowVersionDao.insert(zhzmKnowVersion);


        return zhzmKnowVersion;
    }

    @Transactional
    @Override
    public ZhzmKnowVersion editZhzmKnowVersionEntity(ZhzmKnowVersion zhzmKnowVersion) {
        ZhzmKnowVersion saveZhzmKnowVersion = getZhzmKnowVersionById(zhzmKnowVersion.getId());
        CupkExceptionUtils.throwNullEx(saveZhzmKnowVersion, I18n.msg("知识版本不存在:?", zhzmKnowVersion.getId()));
        saveZhzmKnowVersion.setId(zhzmKnowVersion.getId());
        saveZhzmKnowVersion.setTitle(zhzmKnowVersion.getTitle());
        saveZhzmKnowVersion.setState(zhzmKnowVersion.getState());
        saveZhzmKnowVersion.setCtime(zhzmKnowVersion.getCtime());
        saveZhzmKnowVersion.setCusername(zhzmKnowVersion.getCusername());
        saveZhzmKnowVersion.setCuserkey(zhzmKnowVersion.getCuserkey());
        saveZhzmKnowVersion.setPcontent(zhzmKnowVersion.getPcontent());
        saveZhzmKnowVersion.setKnowid(zhzmKnowVersion.getKnowid());
         
        CupkDbFields.initUpdateBean(saveZhzmKnowVersion, CupkUserContextLoader.getCurrentUser());
        zhzmKnowVersionDao.update(saveZhzmKnowVersion);
        return saveZhzmKnowVersion;
    }

    @Transactional
    @Override
    public ZhzmKnowVersion getZhzmKnowVersionById(String id) {
        return zhzmKnowVersionDao.findById(id);
    }

    @Override
    public List<ZhzmKnowVersion> getZhzmKnowVersions(DataQuery query) {
        return zhzmKnowVersionDao.queryData(query.setCount(false)).getData(ZhzmKnowVersion.class);
    }



    @Transactional
    @Override
    public DataResult searchZhzmKnowVersion(DataQuery query) {
        DataResult result = zhzmKnowVersionDao.queryData(query);
        return result;
    }

    @Override
    public int getZhzmKnowVersionNum(DataQuery query) {
        return zhzmKnowVersionDao.countData(query);
    }


    @Transactional
    @Override
    public void delZhzmKnowVersion(String id) {
        /*[tree：树形结构使用]
        if ( zhzmKnowVersionDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        zhzmKnowVersionDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  zhzmKnowVersionDao.countData(query);
    }

    @Override
    public int getNumByKnowId(String knowid) {
        return zhzmKnowVersionDao.countData(DBRuleList.getInstance().add(new DBRule("KNOWID", knowid, "=")).toList());
    }
    
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            ZhzmKnowVersion node = getZhzmKnowVersionById(sourceId);
            if (!"NONE".equals(targetId)) {
                ZhzmKnowVersion target = getZhzmKnowVersionById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            zhzmKnowVersionDao.update(node);

            List<ZhzmKnowVersion> list = zhzmKnowVersionDao.findSubNodes(sourceId);
            for (ZhzmKnowVersion treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        ZhzmKnowVersion node = zhzmKnowVersionDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(zhzmKnowVersionDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        zhzmKnowVersionDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            ZhzmKnowVersion node =  zhzmKnowVersionDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            zhzmKnowVersionDao.update(node);
        }
    }*/
}
